package com.cblue.qimokao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cblue.qimokao.entity.Film;
import com.cblue.qimokao.mapper.FilmMapper;
import com.cblue.qimokao.service.FilmService;
import com.cblue.qimokao.vo.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class FilmServiceImpl implements FilmService {

    @Resource
    private FilmMapper doctorMapper;

    @Override
    public PageResult findByPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<Film> doctorQueryWrapper = new QueryWrapper<>();
        if(searchMap!=null){
            if(searchMap.get("dept")!=null&&!"".equals(searchMap.get("dept"))){
                doctorQueryWrapper.like("dept","%"+searchMap.get("dept")+"%");
            }
            if(searchMap.get("dname")!=null&&!"".equals(searchMap.get("dname"))){
                doctorQueryWrapper.like("dname","%"+searchMap.get("dname")+"%");
            }
        }
        IPage<Film> iPage = doctorMapper.selectPage(new Page<>(page, size),doctorQueryWrapper);
        return new PageResult(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public void sub(Integer id) {
        Film doctor = doctorMapper.selectById(id);
        if(doctor.getLastnum()>0){
            doctorMapper.sub(id);
        }
    }

    @Override
    public List<Film> findAll() {
        return doctorMapper.selectList(null);
    }

    @Override
    public Integer findLastNum() {
        QueryWrapper<Film> doctorQueryWrapper = new QueryWrapper<>();
        doctorQueryWrapper.eq("dept","万达");
        Film doctor = doctorMapper.selectOne(doctorQueryWrapper);
        return doctor.getLastnum();
    }
}
